<?php
$this->breadcrumbs = array(
    'Products' => array('index'),
);
if ($model->name)
    $this->breadcrumbs = CMap::mergeArray($this->breadcrumbs, array($model->name => array('update', 'id' => $model->id), 'Update'));
else
    $this->breadcrumbs[] = 'Create';

$this->menu = array();

$attrs = $this->getAttributes($model);
?>

<h1><?php echo $model->isNewRecord ? "Create " : "Edit " ?>Product <?php echo $model->name; ?></h1>

<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'product-form',
        'htmlOptions' => array('class' => 'form-horizontal'), // comment this line to use long form
        'enableClientValidation' => true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'beforeValidate'=>'js:function(form){
                if (typeof tinyMCE !== "undefined")
                    $("#Product_details1").val(tinyMCE.activeEditor.getContent());
                return true;
            }',
            'afterValidate'=>'js:function(form, data, hasError){
                if (!hasError)
                {
                    //validate required attribute
                    var valid = true;
                    $.each($("#attributes select"), function(){
                        var attrEle = $(this);
                        var errorMsgEle = attrEle.parent().find(".errorMessage");
                        errorMsgEle.hide();
                        if (attrEle.hasClass("required-attr-group")) {
                            var attrOptsCount = attrEle.find("option").size();
                            if (attrOptsCount == 0) {
                                valid = true;
                            }
                            else if (attrOptsCount == 1) {
                                if (attrEle.find("option").val() == "") {
                                    valid = true;
                                }
                                else if (attrEle.find("option:selected").size() == 0) {
                                    valid = false;
                                    errorMsgEle.show();
                                }
                            }
                            else {
                                if (attrEle.find("option:selected").size() == 0 || attrEle.find("option:selected").val() == "") {
                                    valid = false;
                                    errorMsgEle.show();
                                }
                            }
                        }
                    });
                    if (!valid)
                    {
                        //alert("Attributes are required");
                        //$("a[href=\'#attributes\']").trigger("click");
                        $("ul.nav-tabs li a[href=\"#attributes\"]").trigger("click");
                        return false;
                    }
                }
                return true;
            }',
        )
    )); ?>

    <?php $this->widget('Xpress.components.InfoBox', array('heading' => '')); ?>
    <ul class="nav nav-tabs">
        <li class="active"><a href="#tab1" data-toggle="tab">Product Info</a></li>
        <?php if ($model->isNewRecord) : ?>
            <li><a href="#create-new-subproduct" data-toggle="tab">Sub-Product</a></li>
        <?php endif; ?>
        <?php if ($model->isNewRecord): ?>
            <?php if (count($attrs)) : ?>
                <li><a href="#attributes" data-toggle="tab">Product Attributes</a></li>
            <?php endif; ?>
        <?php else: ?>
            <li><a href="#tab3" data-toggle="tab">Meta Data</a></li>
            <li><a href="#attributes" data-toggle="tab">Product Attributes</a></li>
            <li><a href="#tab4" data-toggle="tab">Sub products</a></li>
            <li><a href="#tab5" data-toggle="tab">Sub-product Attributes Template</a></li>
            <li><a href="#relative" data-toggle="tab">Related Products</a></li>
<!--            <li><a href="#gallery" data-toggle="tab">Gallery</a></li>-->
        <?php endif; ?>
        <!--        <li><a href="#tab2" data-toggle="tab">Addition Details</a></li>-->
    </ul>
    <div class="tab-content">
        <?php if ($model->isNewRecord) : ?>
            <div class="tab-pane" id="create-new-subproduct">
                <?php $this->renderPartial('_form_subproduct',array('form'=>$form,'model'=>$subproduct,'prices'=>$prices)); ?>
            </div>
        <?php endif; ?>
        <?php if (!$model->isNewRecord): ?>
            <div class="tab-pane" id="relative">
                <?php  echo $this->renderPartial('update/_relative', array('model' => $model)); ?>
            </div>
        <?php endif ?>
        <div class="tab-pane active" id="tab1">
            <?php $this->renderPartial('update/_form', array('form' => $form, 'model' => $model)); ?>
        </div>
        <!--        <div class="tab-pane" id="tab2">-->
        <!--            --><?php ////if ($model->isNewRecord) $this->renderPartial('_form_subproduct',array('form'=>$form,'model'=>$subproduct,'prices'=>$prices));?>
        <!--            --><?php //$this->renderPartial('update/_details', array('form' => $form, 'model' => $model)) ?>
        <!--        </div>-->
        <?php if (!$model->isNewRecord): ?>
            <div class="tab-pane" id="tab3">
                <?php $this->renderPartial('update/_meta_data', array('form' => $form, 'metaDataModel' => $metaDataModel)) ?>
            </div>
            <div class="tab-pane" id="tab4">
                <?php echo $this->renderPartial('../subProduct/admin', array('model' => $subProductModel,'subproductProvider'=>$subproductProvider,'modelProduct' => $model, 'subsInfo'=>$subsInfo)); ?>
            </div>
            <div class="tab-pane" id="tab5">
                <?php echo $this->renderPartial('update/_attribute_group', array('model' => $model)); ?>
            </div>
        <?php endif; ?>
        <?php if (!$model->isNewRecord || ($model->isNewRecord && count($attrs))): ?>
            <div class="tab-pane" id="attributes">
                <?php echo $this->renderPartial('update/_attributes', array('model' => $model, 'attrs'=>$attrs)); ?>
            </div>
        <?php endif; ?>
<!--        <div class="tab-pane" id="gallery">-->
<!--            --><?php //$this->renderPartial('update/_gallery', array('form' => $form, 'model' => $model)); ?>
<!--        </div>-->
    </div>
    <div style=" clear:both"></div>
    <div class="control-group buttons">
        <div class="controls">
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class' => 'btn btn-primary productUpdate')); ?>
        </div>
    </div>
    <?php if (!$model->IsNewRecord) echo $form->hiddenField($model, "id");?>
    <?php $this->endWidget() ?>
</div>
<!-- Check validation for subproducts price and salePrice before commit -->
<script type="application/javascript">
    $('.btn.btn-primary.productUpdate').bind('click',function(){
        var errorMess = validateSubmit();
        var content = '<div class=\"alert alert-error\"><button data-dismiss=\"alert\" class=\"close\" type=\"button\"><i class=\"icon-remove\"></i></button><ul class=\"unstyled\"><li>'+errorMess+'</li></ul></div>';
        $('#product-form div.alert.alert-error').remove();
        if(errorMess != ''){
            $('#product-form').prepend(content);
            return false;
        }
    });

</script>